<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Fenxiao_Group extends Stourweb_Controller
{
    public function before()
    {
        parent::before();
    }

    //分组列表
    public function action_list()
    {
        $action=$this->params['action'];
        if(empty($action))
        {
            $this->display('admin/fenxiao/group/list');
        }
        else if($action=='read')
        {
            $node=Arr::get($_GET,'node');
            $list=array();
            if($node=='root')//属性组根
            {

                $list=ORM::factory('fenxiao_group')->get_all();
                array_unshift($list,array('id'=>'0','title'=>'默认分组'));
                foreach($list as $k=>&$v)
                {
                    $v['allowDrag']=false;
                    $v['membernum']= ORM::factory('fenxiao')->where('fxgroupid','=',$v['id'])->count_all();
                    $v['leaf']=true;
                }
                $list[]=array(
                    'leaf'=>true,
                    'id'=>'0add',
                    'title'=>'<button class="btn btn-primary radius size-S" onclick="addSub(0)">添加</button>',
                    'allowDrag'=>false,
                    'allowDrop'=>false,
                    'displayorder'=>'add'
                );
            }
            else //子级
            {

            }
            echo json_encode(array('success'=>true,'text'=>'','children'=>$list));
        }
        else if($action=='addsub')//添加子级
        {
            $pid=Arr::get($_POST,'pid');
            $model=ORM::factory('fenxiao_group');
            $model->title="未命名";
            $model->save();

            if($model->saved())
            {
                $model->reload();
                echo json_encode($model->as_array());
            }
        }
        else if($action=='save') //保存修改
        {
            $rawdata=file_get_contents('php://input');
            $field=Arr::get($_GET,'field');
            $data=json_decode($rawdata);
            $id=$data->id;
            if($field)
            {
                $model=ORM::factory('fenxiao_group',$id);
                if($model->id)
                {
                    $model->$field=$data->$field;
                    $model->save();
                    if($model->saved())
                        echo 'ok';
                    else
                        echo 'no';
                }
            }

        }

        else if($action=='delete')//属性删除
        {
            $rawdata=file_get_contents('php://input');
            $data=json_decode($rawdata);
            $id=$data->id;
            if(!is_numeric($id))
            {
                echo json_encode(array('success'=>false));
                exit;
            }
            $model=ORM::factory('fenxiao_group',$id);
            $model->delete();

        }
        else if($action=='update')//更新操作
        {
            $id=Arr::get($_POST,'id');
            $field=Arr::get($_POST,'field');
            $val=Arr::get($_POST,'val');
            $model=ORM::factory('fenxiao_group',$id);
            if($model->id)
            {
                $model->$field=$val;


                $model->save();
                if($model->saved())
                    echo 'ok';
                else
                    echo 'no';
            }
        }

    }
    
}